package org.example.domain.strategy.service.rule.chain.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.strategy.repository.IStrategyRepository;
import org.example.domain.strategy.service.rule.chain.AbstractRaffleChain;
import org.example.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import org.example.types.common.Constants;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("rule_blacklist")
@Slf4j
public  class BackListLogicChain extends AbstractRaffleChain {

    @Resource
    private IStrategyRepository repository;

    @Override
    protected String ruleModel() {
        return DefaultChainFactory.logicModel.RULE_BLACKLIST.getInfo();
    }

    @Override
    public DefaultChainFactory.strategyAwardVO logic(String userId, Long strategyId) {
        log.info("抽奖责任链-黑名单开始 userId:{} StrategyId:{} ruleModel:{}",userId,strategyId,ruleModel());

        //100:user001,user002,user003
        String ruleValue = repository.queryRuleValueModel(strategyId, ruleModel());
        String[] split = ruleValue.split(Constants.COLON);
        Integer awardId = Integer.parseInt(split[0]);//awardId:100

        /**
         * 过滤个人id
         * 判断抽奖用户是否在其中
         */
        //userIds[]: user001,user002,user003
        String[] userIds = split[1].split(Constants.SPLIT);
        for (String blackListId : userIds) {
            if (userId.equals(blackListId)){
                //用户id在黑名单中 直接返回奖品id
                log.info("抽奖责任链-黑名单接管 userId:{} StrategyId:{} ruleModel:{}",userId,strategyId,ruleModel());
                return DefaultChainFactory.strategyAwardVO.builder()
                        .awardId(awardId)
                        .logicModel(ruleModel())
                        .build();
            }
        }

        log.info("抽奖责任链-黑名单放行 userId:{} StrategyId:{} ruleModel:{}",userId,strategyId,ruleModel());

        return next().logic(userId,strategyId);

    }
}
